//
//  JSContextShare.swift
//  SwiftLua
//
//  Created by xiaerfei on 2019/7/27.
//  Copyright © 2019 xiaerfei. All rights reserved.
//

import Foundation
import JavaScriptCore

final public class JSContextShare {
    static let share = JSContextShare()
    let context:JSContext
    private init() {
        let queue = DispatchQueue(label: "JSContext")
        let vm = queue.sync { JSVirtualMachine()! }
        self.context = JSContext(virtualMachine: vm)
    }
    
    public func setObject(_ object: Any!,forKeyedSubscript: String) {
        self.context.setObject(object, forKeyedSubscript: forKeyedSubscript as NSCopying & NSObjectProtocol)
    }
    
    public func evaluateScript(_ script: String!) -> JSValue! {
        return self.context.evaluateScript(script)
    }
}
